package application;

import zhengqc.stack.ArrayStack;
import zhengqc.stack.Stack;

public class DecimalConvert {
    private int number;
    private int base;
    private Character[] table;
    public DecimalConvert() {
        this.table = new Character[]{
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F'};
    }
    public void set(int number, int base) {
        this.number = number;
        this.base = base;
        if (base <= 1 || base > 16) {
            throw new IllegalArgumentException(String.format("base must in range[%d, %d]", 2, 16));
        }
    }
    public String transform() {
        boolean isNegative = (this.number < 0) ? true : false;
        this.number = Math.abs(this.number);
        Stack<Integer> stack = new ArrayStack<>();
        do {
            stack.push(this.number % this.base);
            this.number = this.number / base;
        } while (this.number != 0);
        StringBuilder sb = new StringBuilder();
        if (isNegative) {
            sb.append('-');
        }
        while ( !stack.isEmpty() ) {
            int t = stack.pop();
            sb.append(this.table[t]);
        }
        return sb.append('[').append("base:").append(this.base).append(']').toString();
    }
}
